using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ADODB;
using PKMCDO;
using SharePoint.ImportExport.Core;

namespace SharePoint.ImportExport.ExportSPS2001		
{
	/// <summary>
	/// Summary description for FolderPicker.
	/// </summary>
	public class frmFolderPicker : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.Button idOK;
		private System.Windows.Forms.Button idCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmFolderPicker()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.idOK = new System.Windows.Forms.Button();
			this.idCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// treeView1
			// 
			this.treeView1.ImageIndex = -1;
			this.treeView1.Location = new System.Drawing.Point(8, 8);
			this.treeView1.Name = "treeView1";
			this.treeView1.SelectedImageIndex = -1;
			this.treeView1.Size = new System.Drawing.Size(280, 248);
			this.treeView1.TabIndex = 0;
			this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
			// 
			// idOK
			// 
			this.idOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.idOK.Location = new System.Drawing.Point(48, 264);
			this.idOK.Name = "idOK";
			this.idOK.TabIndex = 1;
			this.idOK.Text = "&OK";
			this.idOK.Click += new System.EventHandler(this.idOK_Click);
			// 
			// idCancel
			// 
			this.idCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.idCancel.Location = new System.Drawing.Point(152, 264);
			this.idCancel.Name = "idCancel";
			this.idCancel.TabIndex = 2;
			this.idCancel.Text = "&Cancel";
			// 
			// frmFolderPicker
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 302);
			this.Controls.Add(this.idCancel);
			this.Controls.Add(this.idOK);
			this.Controls.Add(this.treeView1);
			this.Name = "frmFolderPicker";
			this.Text = "FolderPicker";
			this.Load += new System.EventHandler(this.frmFolderPicker_Load);
			this.ResumeLayout(false);

		}
		#endregion

		public string startFrom;
		public string selectedFolder;

		/// <summary>
		/// This is the starting point for the folder enumeration
		/// </summary>
		public string StartFrom
		{
			set
			{
				startFrom=value;
			}
		}

		/// <summary>
		/// This is the selected folder
		/// </summary>
		public string SelectedFolder
		{
			get
			{
				return selectedFolder;
			}
		}

		public string workspaceName;


		private void addNodes(ConnectionClass Conn, TreeNode parent,string folderUrl)
		{
			string query= "SELECT \"DAV:href\", \"DAV:displayname\", \"DAV:hassubs\" from scope('shallow traversal of \"" +folderUrl+ "\"') WHERE \"DAV:isfolder\" = true ORDER BY \"DAV:displayname\" ";
			query=query.Replace("//","/");
			ADODB.RecordsetClass rs= new ADODB.RecordsetClass();
			rs.Open(query, Conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);
			while(!rs.EOF)
			{
				string thisFolderName=rs.Fields["DAV:displayname"].Value.ToString();
				TreeNode thisNode=parent.Nodes.Add(thisFolderName);
				bool b=bool.Parse(rs.Fields["DAV:hassubs"].Value.ToString());
				if(b)
					addNodes(Conn,thisNode,folderUrl+"/"+thisFolderName);

					
				Logger.Log(ErrorLevel.Progress,rs.Fields["DAV:displayname"].Value.ToString());
				rs.MoveNext();
			}
		}
		private void frmFolderPicker_Load(object sender, System.EventArgs e)
		{
			treeView1.PathSeparator="/";

			ConnectionClass Conn = new ConnectionClass();
			try
			{
				Conn.ConnectionString = "provider=msdaipp.dso";
				Conn.CommandTimeout = 0;
				Conn.Open(workspaceName,null,null,0);
				while(startFrom.StartsWith("/"))
					startFrom=startFrom.Substring(1);

				TreeNode node=treeView1.Nodes.Add(startFrom);
				string folderURL=workspaceName.Substring(workspaceName.LastIndexOf("/"))+"/"+ startFrom;

				string query= "SELECT \"DAV:href\", \"DAV:displayname\", \"DAV:hassubs\" from scope('shallow traversal of \"" +folderURL+ "\"') WHERE \"DAV:isfolder\" = true ORDER BY \"DAV:displayname\" ";
				query=query.Replace("//","/");
				ADODB.RecordsetClass rs= new ADODB.RecordsetClass();
				rs.Open(query, Conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);
				while(!rs.EOF)
				{
					string thisFolderName=rs.Fields["DAV:displayname"].Value.ToString();
					TreeNode thisNode=node.Nodes.Add(thisFolderName);
					string s=rs.Fields["DAV:hassubs"].Value.ToString();
					bool b=bool.Parse(s);
					if(b)
						addNodes(Conn,thisNode,folderURL+"/"+thisFolderName);

					
					Logger.Log(ErrorLevel.Progress,rs.Fields["DAV:displayname"].Value.ToString());
					rs.MoveNext();
				}




			}
			catch(Exception Ex)
			{
				Logger.Log(ErrorLevel.Error, "Error when enumerating through the folders. Error: "+Ex.Message);
			}
			finally
			{
				if(Conn.State==1)
					Conn.Close();
				
			}


		}

		private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
		
		}

		private void idOK_Click(object sender, System.EventArgs e)
		{
			TreeNode node=treeView1.SelectedNode;
			selectedFolder="/"+node.FullPath;

			Close();
		}

		/// <summary>
		/// This is the name of the workspace
		/// </summary>
		public string WorkspaceName
		{
			set
			{
				workspaceName=value;
			}
		}
	}
}
